1 //==============================================================================
2 // file : ResourcesReader.java
3 // project: Java Common Utility
4 //
5 // last change: date: $Date: 2003/09/10 09:22:14 $
6 // by: $Author: bitiboy $
7 // revision: $Revision: 1.1 $
8 //------------------------------------------------------------------------------
9 // copyright: GNU GPL Software License (see class documentation)
10 //==============================================================================
11 package com.justhis.util;
12
13
14 /*
15 * $Id: ResourcesReader.java,v 1.1 2003/09/10 09:22:14 bitiboy Exp $
16 *
17 * Copyright 2003 Acai Software All Rights Reserved.
18 *
19 * This file ResourcesReader.java is part of the Java Common Utility.
20
21 * The Java Common Utility is free software; you can redistribute it and/or modify
22 * it under the terms of the GNU General Public License as published by
23 * the Free Software Foundation; either version 2 of the License, or
24 * (at your option) any later version.
25
26 * Java Common Utility is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 * GNU General Public License for more details.
30
31 * You should have received a copy of the GNU General Public License
32 * along with the Java Common Utility; if not, write to the Free Software
33 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
34
35 * http://www.justhis.com
36 * CONTACT: email = superaxis@sohu.com webmaster@justhis.com
37 */
38 import java.text.MessageFormat;
39
40 import java.util.Locale;
41 import java.util.MissingResourceException;
42 import java.util.ResourceBundle;
43
44
45 /***
46 * TODO
47 *
48 * @author <a href="http://blog.ejb.cn">acai</a>
49 * @version $Revision: 1.1 $
50 */
51 public class ResourcesReader {
52 //~ Static fields/initializers ---------------------------------------------
53
54 /*** resouce bundle */
55 private static ResourceBundle resource_bundle;
56
57 /*** a locale to record client locale */
58 private static Locale locale;
59
60 static {
61 initBundle();
62 }
63
64 //~ Methods ----------------------------------------------------------------
65
66 /***
67 * Get the country part of a locale encoding
68 *
69 * @param encoding TODO
70 *
71 * @return TODO
72 */
73 public static String getCountry(String encoding) {
74 if (encoding == null) {
75 return null;
76 }
77
78 if (encoding.indexOf("_") < 0) {
79 return null;
80 }
81
82 String result = encoding.substring(encoding.indexOf("_") + 1,
83 encoding.length()
84 );
85
86 if (result.indexOf("_") < 0) {
87 return result.trim();
88 }
89
90 return result.substring(0, encoding.indexOf("_"));
91 }
92
93 /***
94 * Get the language part of a locale encoding
95 *
96 * @param encoding TODO
97 *
98 * @return TODO
99 */
100 public static String getLanguage(String encoding) {
101 if (encoding == null) {
102 return null;
103 }
104
105 if (encoding.indexOf("_") < 0) {
106 return encoding.trim();
107 }
108
109 return encoding.substring(0, encoding.indexOf("_"));
110 }
111
112 /***
113 * Set the Locale using a java class instance of a locale
114 *
115 * @param locale TODO
116 */
117 public static void setLocale(Locale locale) {
118 try {
119 System.out.println("Now Set Locale as :" + locale.toString());
120 ResourcesReader.locale = locale;
121 ResourcesReader.resource_bundle = ResourceBundle.getBundle("Bundle",
122 locale
123 );
124 } catch (MissingResourceException mre) {
125 System.err.println("Missing Resource for locale, "
126 + locale.toString()
127 );
128 mre.printStackTrace(System.err);
129 }
130 }
131
132 /***
133 * Set the Locale using a string encoding of a locale. Encodings are of the
134 * form: country_language_variant with only the country parameter being
135 * required. For example: en_US represents English in the united states,
136 * de_DE represents German in Germany.
137 *
138 * @param encoding TODO
139 */
140 public static void setLocale(String encoding) {
141 /*String language = getLanguage(encoding);
142 String country = getCountry(encoding);
143 String variant = getVariant(encoding);
144
145 if ((variant == null) || variant.trim().equals("")) {
146 setLocale(new Locale(language, country));
147 } else {
148 setLocale(new Locale(language, country, variant));
149 }*/
150 setLocale(new Locale(encoding));
151 }
152
153 /***
154 * Return the instance of the current locale.
155 *
156 * @return TODO
157 */
158 public static Locale getLocale() {
159 if (ResourcesReader.locale == null) {
160 ResourcesReader.initBundle();
161 }
162
163 return ResourcesReader.locale;
164 }
165
166 /***
167 * Return the encoding of the current locale.
168 *
169 * @return TODO
170 */
171 public static String getLocaleEncoding() {
172 if (ResourcesReader.locale == null) {
173 ResourcesReader.initBundle();
174 }
175
176 return ResourcesReader.locale.toString();
177 }
178
179 /***
180 * Lookup the translation of a particular key
181 *
182 * @param key TODO
183 *
184 * @return TODO
185 */
186 public static String getTranslation(String key) {
187 if ((key == null) || (resource_bundle == null)) {
188 return "";
189 }
190
191 try {
192 String retStr = resource_bundle.getString(key);
193
194 return retStr;
195 } catch (Exception e) {
196 return key;
197 }
198 }
199
200 /***
201 * Lookup the translation of a particular key that contains exactly one
202 * lookup item within its translation
203 *
204 * @param key TODO
205 * @param lookup TODO
206 *
207 * @return TODO
208 */
209 public static String getTranslation(String key, String lookup) {
210 if ((key == null) || (resource_bundle == null)) {
211 return "";
212 }
213
214 try {
215 Object[] objects = { lookup };
216 String retStr = resource_bundle.getString(key);
217 retStr = MessageFormat.format(retStr, objects);
218
219 return retStr;
220 } catch (Exception e) {
221 return key;
222 }
223 }
224
225 /***
226 * Lookup the translation of a particular key that contains any number of
227 * lookup items within its translation
228 *
229 * @param key TODO
230 * @param lookup TODO
231 *
232 * @return TODO
233 */
234 public static String getTranslation(String key, String[] lookup) {
235 if ((key == null) || (resource_bundle == null)) {
236 return "";
237 }
238
239 try {
240 Object[] objects = new Object[lookup.length];
241
242 for (int i = 0; i < lookup.length; i++) {
243 objects[i] = lookup[i];
244 }
245
246 String retStr = resource_bundle.getString(key);
247 retStr = MessageFormat.format(retStr, lookup);
248
249 return retStr;
250 } catch (Exception e) {
251 return key;
252 }
253 }
254
255 /***
256 * Get the variant part of a locale encoding
257 *
258 * @param encoding TODO
259 *
260 * @return TODO
261 */
262 public static String getVariant(String encoding) {
263 if (encoding == null) {
264 return null;
265 }
266
267 if (encoding.indexOf("_") < 0) {
268 return null;
269 }
270
271 String result = encoding.substring(encoding.indexOf("_") + 1,
272 encoding.length()
273 );
274
275 if (result.indexOf("_") < 0) {
276 return null;
277 }
278
279 result = result.substring(result.indexOf("_") + 1, result.length());
280
281 return result.trim();
282 }
283
284 /***
285 * Initialize the bundle to use the resources associated with the default
286 * locale specified by the JVM
287 */
288 public static void initBundle() {
289 try {
290 if (locale == null) {
291 locale = Locale.getDefault();
292 }
293
294 resource_bundle = ResourceBundle.getBundle("Bundle", locale);
295 } catch (MissingResourceException mre) {
296 System.err.println("Missing Resource for default locale, "
297 + Locale.getDefault().toString()
298 );
299 mre.printStackTrace(System.err);
300 }
301 }
302 }
303
304
305 /*
306 * $Log: ResourcesReader.java,v $
307 * Revision 1.1 2003/09/10 09:22:14 bitiboy
308 * *** empty log message ***
309 *
310 *
311 */
This page was automatically generated by Maven